package com.wlyuan.open.controller;

import com.wlyuan.core.exception.ValueError;
import com.wlyuan.open.api.IOpenClientService;
import com.wlyuan.open.application.sms.SmsCodeValidate;
import com.wlyuan.open.application.sms.service.SmsService;
import com.wlyuan.open.constants.SmsType;
import com.wlyuan.open.dto.api.OpenClientBindDTO;
import com.wlyuan.open.dto.api.OpenClientChangeAppSecretDTO;
import com.wlyuan.open.dto.api.OpenClientLoginDTO;
import com.wlyuan.open.dto.api.OpenClientUnbindMobileDTO;
import com.wlyuan.open.vo.OpenClientDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author yuanjie
 */
@Slf4j
@Api(tags = "客户端")
@RequestMapping("/client")
@Validated
@RestController
@RequiredArgsConstructor
public class OpenClientController extends BaseController {
    private final SmsService smsService;
    @DubboReference(check = false)
    private IOpenClientService openClientService;

    /*@ApiOperation("注册")
    @PostMapping("/register")
    public OpenClientDetailVO register(@RequestBody @Valid OpenClientRegisterDTO register) {
        return openClientService.register(register);
    }*/

    @ApiOperation("查看")
    @GetMapping("")
    public OpenClientDetailVO getOpenClientByAppId(@RequestParam("appId") String appId, @RequestParam("appSecret") String appSecret) {
        var client = openClientService.getOpenClientByAppId(appId);
        if (client == null) {
            throw new ValueError("开放租户不存在");
        }
        return client;
    }

    @ApiOperation("注册")
    @PostMapping("/register")
    public OpenClientDetailVO register(@RequestBody OpenClientLoginDTO register) {
        smsService.validate(new SmsCodeValidate(SmsType.REGISTER.getValue(), register.getMobile(), register.getCode()));
        return openClientService.register(register);
    }


    @ApiOperation("登录")
    @PostMapping("/login")
    @Deprecated
    public OpenClientDetailVO login(@RequestBody OpenClientLoginDTO loginDTO) {
        return openClientService.login(loginDTO);
    }

    @ApiOperation("换绑手机号")
    @PostMapping("/mobile/unbind")
    public OpenClientDetailVO unbind(@RequestBody OpenClientUnbindMobileDTO unbindMobile) {
        unbindMobile.setAppId(getAppId());
        smsService.validate(new SmsCodeValidate(SmsType.UNBIND_MOBILE.getValue(), unbindMobile.getMobile(), unbindMobile.getCode()));
        return openClientService.unbindMobile(unbindMobile);
    }

    @ApiOperation("根据clientId查看")
    @GetMapping("/getClient")
    public OpenClientDetailVO getOpenClientByClientId() {
        var appId = getAppId();
        return openClientService.getOpenClient(appId);
    }

    @ApiOperation("绑定企业版管理员账号")
    @PostMapping("/bind")
    public OpenClientDetailVO bind(@RequestBody OpenClientBindDTO bind) {
        //bind.setAppId(getAppId());
        smsService.validate(new SmsCodeValidate(SmsType.BIND.getValue(), getAccountMobile(bind.getAccount()), bind.getCode()));
        return openClientService.bind(bind);
    }

    @ApiOperation("修改appSecret")
    @PostMapping("/change")
    public OpenClientDetailVO change(@RequestBody OpenClientChangeAppSecretDTO change) {
        smsService.validate(new SmsCodeValidate(SmsType.CHANGE_APP_SECRET.getValue(), getOpenMobile(), change.getCode()));
        change.setAppId(getAppId());
        return openClientService.changeAppSecret(change);
    }

    @ApiOperation("获取appSecret")
    @PostMapping("/getSecret")
    public String getAppSecret() {
        return openClientService.getAppSecret(getAppId());
    }
}
